<template>
  <div>
    <el-card>
      <template #header> 商品添加 </template>
      <el-form label-width="80px" style="width: 300px">
        <el-form-item label="商品名称">
          <el-input placeholder="商品名称" v-model="formData.name"> </el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="category">
            <el-option
              v-for="item in formData.category"
              :key="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input-number
            v-model="formData.price"
            :min="1"
            :max="10"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            action="http://sell.h5.itsource.cn:8087/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="formData.imgUrl" :src="formData.imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="formData.goodsDesc"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { api_goods_add } from "@/api/user";
import { api_goods_categories } from "@/api/goods";
export default {
  data() {
    return {
      formData: {
        name: "",
        category: [],
        price: "",
        imgUrl: "",
        goodsDesc: "",
      },
      category: "",
    };
  },
  methods: {
    handleAvatarSuccess(res) {
      let { code, msg, imgUrl } = res;
      if (code === 0) {
        this.formData.imgUrl = imgUrl;
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //添加商品
    async addGoods() {
      let res = await api_goods_add({
        ...this.formData,
        category: this.category,
      });
      let { code, msg } = res.data;
      if (code === 0) {
        this.$router.push("/goods/goods-list");
      }
    },
    // 初始化获取已启用分类列表
    async getCateList() {
      let res = await api_goods_categories();
      this.formData.category = res.data;
    },
  },
  created() {
    this.getCateList();
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader :deep(.el-upload) {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>